<template>
  <div class="topbar">
    <div class="wrapTopbar wrap">
      <div class="mallTick">欢迎来到叩丁狼积分商城</div>
      <ul class="nav">
        <li>
          <img src="../assets/img/userImg.f8bbec5e.png" class="pict" alt="" />
        </li>
        <li>用户名：--</li>
        <li>我的积分：--</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isLogin">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>120</strong>
        </li>
        <li class="login" @click="changeIsShowModal(true)" v-else>登录</li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from "vuex";
import { WXLoginAPI } from "@/request/api";
export default {
  data() {
    return {};
  },
  async created() {
    let myCode = this.$route.query.code;
    if (!myCode) {
      return;
    }
    let res = await WXLoginAPI({
      code: myCode,
    });
    console.log(res);
    if (res.code == 0) {
      console.log(res);
      // 保存token值
      localStorage.setItem("x-auth-token", res["x-auth-token"]);
      // 登录成功,隐藏模态框
      this.asyncChangeIsShowToast({
        msg: "登录成功",
        type: "success",
      });
      this.changeIsShowModal(false);
      // 更新登录状态
      this.changeIsLogin(true);
      // 清除code值
      this.$router.push(this.$route.path);
    } else if (res.code == 400) {
      // 重新刷新并扫描二维码
      this.asyncChangeIsShowToast({
        msg: "重新刷新并扫描二维码",
        type: "warning",
      });
    } else {
      this.asyncChangeIsShowToast({
        msg: "先使用手机号登录绑定微信",
        type: "warning",
      });
      // uuid
      localStorage.setItem("uuid", res.uuid);
    }
  },
  computed: {
    ...mapState({
      isShowModal: (state) => state.showModal.isShowModal,
      isLogin: (state) => state.showLogin.isLogin,
    }),
  },

  methods: {
    ...mapMutations({
      changeIsShowModal: "showModal/changeIsShowModal",
      changeIsLogin: "showLogin/changeIsLogin",
    }),
    ...mapActions({
      asyncChangeIsShowToast: "showToast/asyncChangeIsShowToast",
    }),
  },
};
</script>

<style lang="less" scoped>
.topbar {
  width: 100%;
  height: 40px;
  background: #242b39;

  .wrapTopbar {
    height: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    div.mallTick {
      width: 155px;
      font-size: 14px;
      font-family: SourceHanSansSC;
      font-weight: 300;
      color: #fffefe;
    }
    ul.nav {
      height: 100%;
      font-size: 14px;
      font-family: SourceHanSansSC;
      font-weight: 300;
      color: #fffefe;
      display: flex;
      justify-content: space-between;

      li {
        padding: 0 5px;
        box-sizing: border-box;
        height: 40px;
        line-height: 40px;
        cursor: pointer;
        .pict {
          width: 23px;
          height: 22px;
          border-radius: 13px;
          vertical-align: middle;
        }
        &.login {
          width: 124px;
          text-align: center;
          background: #0a328e;
          font-size: 14px;
          font-family: SourceHanSansSC;
          font-weight: 300;
          color: #fffefe;
        }
      }
      .cart-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        text-align: center;
        img {
          width: 20px;
          height: 19px;
          vertical-align: middle;
        }
        span {
          margin: 0 8px;
        }
        strong {
          width: 22px;
          height: 22px;
          line-height: 22px;
          display: inline-block;
          border-radius: 50%;
          background-color: #fd604d;
        }
      }
    }
  }
}
</style>
